package de.busschedule.control;

import android.app.Activity;
import android.content.res.Resources;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckedTextView;
import android.widget.TextView;
import android.widget.Toast;
import de.busschedule.R;

public class ExpandableListAdapter extends BaseExpandableListAdapter{
	private final SparseArray<Group> groups;
	public LayoutInflater inflater;
	public Activity activity;

	public ExpandableListAdapter(Activity act, SparseArray<Group> groups){
		activity = act;
		this.groups = groups;
		inflater = act.getLayoutInflater();
	}

	@Override
	public Object getChild(int groupPosition, int childPosition){
		return groups.get(groupPosition).children.get(childPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition){
		return 0;
	}

	@Override
	public View getChildView(int groupPosition, final int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent){
		final String children = (String) getChild(groupPosition, childPosition);
		if(convertView == null){
			convertView = inflater.inflate(R.layout.list_row_linedetail, null);
		}
		
		//Get name and time
		String[] s = children.split("\\#");
		
		if(s.length == 2){
			Resources res = convertView.getResources();
			String str = res.getString(R.string.next_departure);
			//append to layout
			TextView nameField = (TextView) convertView.findViewById(R.id.list_row_line_detail_name);
			nameField.setText(s[0]);
			TextView timeField = (TextView) convertView.findViewById(R.id.list_row_line_detail_time);
			timeField.setText(str + s[1]);
		}
		
		convertView.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v){
				Toast.makeText(activity, children, Toast.LENGTH_SHORT).show();
			}
		});
		return convertView;
	}

	@Override
	public int getChildrenCount(int groupPosition){
		return groups.get(groupPosition).children.size();
	}

	@Override
	public Object getGroup(int groupPosition){
		return groups.get(groupPosition);
	}

	@Override
	public int getGroupCount(){
		return groups.size();
	}

	@Override
	public void onGroupCollapsed(int groupPosition){
		super.onGroupCollapsed(groupPosition);
	}

	@Override
	public void onGroupExpanded(int groupPosition){
		super.onGroupExpanded(groupPosition);
	}

	@Override
	public long getGroupId(int groupPosition){
		return 0;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent){
		if(convertView == null){
			convertView = inflater.inflate(R.layout.expandable_list_row_group, null);
		}
		Group group = (Group) getGroup(groupPosition);
		((CheckedTextView) convertView).setText(group.string);
		((CheckedTextView) convertView).setChecked(isExpanded);
		return convertView;
	}

	@Override
	public boolean hasStableIds(){
		return false;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition){
		return false;
	}
}